<?php

/**
 *   OPENSHOP_taobao 商品相关设置
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: api_tb_app.php,v 1.0 2009/11/5 07:35:58 add $
 *   @author      winner <jsycywm@hotmail.com>
 */

define('IN_OS', true);

require_once(dirname(__FILE__).'/includes/init.php');

/*------------------------------------------------------ */
//-- 列表编辑 ?act=list_edit
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list_edit')
{
//	$last_1  = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
//	$start_time = date("Y-m-d H:m:s",$last_1);
//	echo $start_time;
	
//	$tomorrow = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
//	$end_time = date("Y-m-d H:m:s",$tomorrow);
//	echo $end_time;
	
	
    /* 检查权限 */
    admin_priv();

    $smarty->assign('ur_here',      '淘宝接口设置');
    
    $item_list = get_tb_settings(null);  
    //$smarty->assign('tb_bbnamestr',  get_code_value($item_list,'tb_bbnamestr') );  
    $smarty->assign('unicolor',  get_code_value($item_list,'unicolor') ); 
    $smarty->assign('unisize' ,  get_code_value($item_list,'unisize') );  
	$smarty->assign('use_postscript',  get_code_value($item_list,'use_postscript') ); 
	$smarty->assign('use_postscript_have',  get_code_value($item_list,'use_postscript_have') );
	$smarty->assign('use_postscript_have_lock',  get_code_value($item_list,'use_postscript_have_lock') );	
	$smarty->assign('use_postscript_no',  get_code_value($item_list,'use_postscript_no') );
	$smarty->assign('use_buss_desc',  get_code_value($item_list,'use_buss_desc') );
	$smarty->assign('use_buss_desc_no',  get_code_value($item_list,'use_buss_desc_no') );
	$smarty->assign('use_buss_desc_no_lock',  get_code_value($item_list,'use_buss_desc_no_lock') );   
	$smarty->assign('use_auto_merge',  get_code_value($item_list,'use_auto_merge') );
	$smarty->assign('use_auto_merge_time',  get_code_value($item_list,'use_auto_merge_time') ); 
	$smarty->assign('use_auto_cut',  get_code_value($item_list,'use_auto_cut') );
	$smarty->assign('use_auto_cut_actuals_sell',  get_code_value($item_list,'use_auto_cut_actuals_sell') ); 
    $smarty->assign('update_inventory_time',  get_code_value($item_list,'auto_upload_top_time') );
    $smarty->assign('lock_tag',  get_code_value($item_list,'lock_tag') );
    $smarty->assign('download_tag',  get_code_value($item_list,'download_tag') );  
    $smarty->assign('auto_upload_top',  get_code_value($item_list,'auto_upload_top') ); 
	$smarty->assign('setinterval',  get_code_value($item_list,'setinterval') );//任务计划时间		setinterval	分钟 
	$smarty->assign('auto_update_goods',  get_code_value($item_list,'auto_update_goods') );  //自动更新商品(注:更新商品，名称，描述，图片,价格等) 
	$smarty->assign('auto_listing_delisting',  get_code_value($item_list,'auto_listing_delisting') );//自动上下架		auto_listing_delisting
	$smarty->assign('auto_traderates_get_buyer',  get_code_value($item_list,'auto_traderates_get_buyer') );//自动下载买家评价	auto_traderates_get_buyer	
	$smarty->assign('auto_delay_TOP_SendOrder',  get_code_value($item_list,'auto_delay_TOP_SendOrder') );//自动同步异常发货	auto_delay_TOP_SendOrder
    $smarty->assign('Refund_tag',  get_code_value($item_list,'Refund_tag') );
    //解析数据字段，以供展示用
    $str_value = get_code_value($item_list,'auto_deal_time');
    $str_arr = explode(';',$str_value);
    $smarty->assign('recent_time',$str_arr[0]);
    if($str_arr[0]==1){
    	$smarty->assign('recent_time_select',$str_arr[1]);
    }elseif ($str_arr[0]==2){ 
    	$str_temp = explode('||',$str_arr[1]); 
    	$smarty->assign('start_date',$str_temp[0]);   
    	$smarty->assign('end_date',$str_temp[1]); 
    }
    //退单时间控制
    $r_str_value = get_code_value($item_list,'r_auto_deal_time');
    $str_arr = explode(';',$r_str_value);
    $smarty->assign('r_recent_time',$str_arr[0]);
    if($str_arr[0]==1){
    	$smarty->assign('r_recent_time_select',$str_arr[1]);
    }elseif ($str_arr[0]==2){ 
    	$str_temp = explode('||',$str_arr[1]); 
    	$smarty->assign('r_start_date',$str_temp[0]);   
    	$smarty->assign('r_end_date',$str_temp[1]); 
    }
    $status = get_code_value($item_list,'status');
    $status_keys = explode(';',$status);
    foreach ($status_keys as $stats)
    {
    	$smarty->assign($stats, $stats);
    } 
    $smarty->assign('status',  get_code_value($item_list,'status') );
    
    $r_status = get_code_value($item_list,'r_status');
    $status_keys = explode(';',$r_status); 
    foreach ($status_keys as $stats)
    {
    	$smarty->assign($stats, $stats);
    } 
    $smarty->assign('r_status',  get_code_value($item_list,'r_status') );
    $smarty->display("api_tb_config.tpl");
}

/*------------------------------------------------------ */
//-- 提交   ?act=post
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'post')
{
 	 //print_r($_REQUEST);
 	 //die();//update_inventory_time 
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['bbnamestr'] . "' WHERE code = 'tb_bbnamestr'";
	$db->query($sql); 	 
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['unicolor'] . "' WHERE code = 'unicolor'";
	$db->query($sql); //均色
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['unisize'] . "' WHERE code = 'unisize'";
	$db->query($sql); //均码 
    $sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_postscript'] . "' WHERE code = 'use_postscript'";
    $db->query($sql);  //启用买家留言
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_postscript_have'] . "' WHERE code = 'use_postscript_have'";
	$db->query($sql); //已留言   1确认,2未确认
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_postscript_have_lock'] . "' WHERE code = 'use_postscript_have_lock'";
	$db->query($sql); //有留言并挂起
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_postscript_no'] . "' WHERE code = 'use_postscript_no'";
	$db->query($sql); //未留言    1确认,2未确认
	
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_buss_desc'] . "' WHERE code = 'use_buss_desc'";
	$db->query($sql); //启用商家备注
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_buss_desc_no'] . "' WHERE code = 'use_buss_desc_no'";
	$db->query($sql); //未备注1直接转 2不转
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_buss_desc_no_lock'] . "' WHERE code = 'use_buss_desc_no_lock'";
	$db->query($sql); // 未备注挂起 
	
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_auto_merge'] . "' WHERE code = 'use_auto_merge'";
	$db->query($sql); //use_auto_merge启用
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_auto_merge_time'] . "' WHERE code = 'use_auto_merge_time'";
	$db->query($sql); //use_auto_merge_time自动合单时间
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_auto_cut'] . "' WHERE code = 'use_auto_cut'";
	$db->query($sql); //use_auto_cut启用
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['use_auto_cut_actuals_sell'] . "' WHERE code = 'use_auto_cut_actuals_sell'";
	$db->query($sql); //use_auto_cut_actuals_sell  启用自动折单 条件 现货预售并存  
	

    $sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['update_inventory_time'] . "' WHERE code = 'auto_upload_top_time'";
    $db->query($sql);
    $sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['Refund_tag'] . "' WHERE code = 'Refund_tag'";
    $db->query($sql);
	//更新挂起标志位
    $sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['lock_tag'] . "' WHERE code = 'lock_tag'";
    $db->query($sql);
    //更新下载标志位
    $sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['download_tag'] . "' WHERE code = 'download_tag'";
    $db->query($sql);
	//任务计划时间		setinterval	分钟
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['setinterval'] . "' WHERE code = 'setinterval'";
	$db->query($sql);
	//自动上下架		auto_listing_delisting
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['auto_listing_delisting'] . "' WHERE code = 'auto_listing_delisting'";
	$db->query($sql);
	//自动下载买家评价	auto_traderates_get_buyer	
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['auto_traderates_get_buyer'] . "' WHERE code = 'auto_traderates_get_buyer'";
	$db->query($sql);
	//自动同步异常发货	auto_delay_TOP_SendOrder
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['auto_delay_TOP_SendOrder'] . "' WHERE code = 'auto_delay_TOP_SendOrder'";
	$db->query($sql); 
 	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['auto_upload_top'] . "' WHERE code = 'auto_upload_top'";
	$db->query($sql);   //更新库存
	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $_POST['auto_update_goods'] . "' WHERE code = 'auto_update_goods'";
    $db->query($sql);  //更新商品
	$r_stats = "";  //退单状态
    $s_stats = "";
    if($_REQUEST['ALL_CLOSED']){   //  ALL_CLOSED(包含：TRADE_CLOSED、TRADE_CLOSED_BY_TAOBAO) 
    	$s_stats = $s_stats.'ALL_CLOSED;';
    }
    if($_REQUEST['ALL_WAIT_PAY']){   // ALL_WAIT_PAY(包含：WAIT_BUYER_PAY、TRADE_NO_CREATE_PAY)  
    	$s_stats = $s_stats.'ALL_WAIT_PAY;';
    }
    if($_REQUEST['WAIT_BUYER_PAY']){     
    	$s_stats = $s_stats.'WAIT_BUYER_PAY;';  //可以不用 ALL_WAIT_PAY代替
    }
    if ($_REQUEST['TRADE_NO_CREATE_PAY']){
    	$s_stats = $s_stats.'TRADE_NO_CREATE_PAY;'; //可以不用ALL_WAIT_PAY代替
    }
    if ($_REQUEST['WAIT_BUYER_CONFIRM_GOODS']){
    	$s_stats = $s_stats.'WAIT_BUYER_CONFIRM_GOODS;';
    }
    if ($_REQUEST['WAIT_SELLER_SEND_GOODS']){
    	$s_stats = $s_stats.'WAIT_SELLER_SEND_GOODS;';
    }
    if ($_REQUEST['TRADE_FINISHED']){
    	$s_stats = $s_stats.'TRADE_FINISHED;';
    }
    if ($_REQUEST['TRADE_CLOSED']){
    	$s_stats = $s_stats.'TRADE_CLOSED;';
    }
    if ($_REQUEST['TRADE_CLOSED_BY_TAOBAO']){
    	$s_stats = $s_stats.'TRADE_CLOSED_BY_TAOBAO;';
    }
    if ($_REQUEST['OTHER']){
    	$s_stats = $s_stats.'OTHER;';
    }
    if ($_REQUEST['ALL']){
    	$s_stats = $s_stats.'ALL;';
    } 
    //更新状态标志位
 	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $s_stats . "' WHERE code = 'status'";
    $db->query($sql);  
   if($_REQUEST['WAIT_SELLER_AGREE']){    
    	$r_stats  = $r_stats.'WAIT_SELLER_AGREE;';
    }
    if($_REQUEST['WAIT_BUYER_RETURN_GOODS']){   
    	$r_stats  = $r_stats.'WAIT_BUYER_RETURN_GOODS;';
    }
    if($_REQUEST['WAIT_SELLER_CONFIRM_GOODS']){   
    	$r_stats  = $r_stats.'WAIT_SELLER_CONFIRM_GOODS;';
    }
    if($_REQUEST['SELLER_REFUSE_BUYER']){   
    	$r_stats  = $r_stats.'SELLER_REFUSE_BUYER;';
    }
    if($_REQUEST['CLOSED']){   
    	$r_stats  = $r_stats.'CLOSED;';
    }
    if($_REQUEST['SUCCESS']){    
    	$r_stats  = $r_stats.'SUCCESS;';
    } 
 	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $r_stats . "' WHERE code = 'r_status'";
    $db->query($sql); //更新状态标志位 
    //更新转换时间段
    $str_value = '0;0';
    if ($_REQUEST['time']=="1"){
    	$str_value = "1;".$_REQUEST['recent_time_select'];
    }elseif ($_REQUEST['time']=="2"){ 
//    	$str_value = "2;".$_REQUEST['start_date']." ".$_REQUEST['search_start_created_time'];
		$str_value = "2;".$_REQUEST['start_date']   ;
    	$str_value = $str_value.'||';
    	$str_value = $str_value.$_REQUEST['end_date'] ;
    }else{
    	$str_value = '0;0';
    }
 	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $str_value . "' WHERE code = 'auto_deal_time'";
    $db->query($sql);
    
    //更新转换退单时间段
    $r_str_value = '0;0';
    if ($_REQUEST['r_time']=="1"){
    	$r_str_value = "1;".$_REQUEST['r_recent_time_select'];
    }elseif ($_REQUEST['r_time']=="2"){  
		$r_str_value = "2;".$_REQUEST['r_start_date']   ;
    	$r_str_value = $r_str_value.'||';
    	$r_str_value = $r_str_value.$_REQUEST['r_end_date'] ;
    }else{
    	$r_str_value = '0;0';
    }
 	$sql = "UPDATE " . $os->table('tb_config') . " SET value = '" . $r_str_value . "' WHERE code = 'r_auto_deal_time'";
    $db->query($sql);
    
    /* 记录日志 */
    admin_log('', 'edit', 'tb_config'); 
    $links[] = array('text' => '返回淘宝接口设置', 'href' => 'api_tb_config.php?act=list_edit');
    sys_msg('保存成功。', 0, $links);

}

/**
 * 获得设置信息
 *
 * @param   array   $groups     需要获得的设置组
 * @param   array   $excludes   不需要获得的设置组
 *
 * @return  array
 */
function get_tb_settings($code)
{
    global $db, $os;
	
    if($code){
    	/* 取出全部数据：分组和变量 */
    	$sql = "SELECT * FROM " . $GLOBALS['os']->table('tb_config') .
            " WHERE code = '".$code."'";
    }else{
    	$sql = "SELECT * FROM " . $GLOBALS['os']->table('tb_config');
    }

    $item_list = $db->getAll($sql);

    return $item_list;
}

function get_code_value($item_list,$code)
{
	foreach ($item_list as $item)
	{
    	if($item['code']==$code)
    		return $item['value'];
    }
    return "";
}

?>